<?php
if ($_POST['submit'] != ""):
	switch ($_POST['data_engine']):
		case "mysql":
			//链接数据库
			require "../Include/Db.class.php";
			$db = Db::getInstance();
			$db->dbHost = $_POST['host'];
			$db->dbName = $_POST['dbname'];
			$db->dbUser = $_POST['uid'];
			$db->dbPwd = $_POST['pwd'];
			$db->open();
			$tb_prefix = "Tables_in_" . $_POST['dbname'];
			if ($_POST['table'] == ""):
				$tables = $db->getArray($db->select("SHOW TABLES FROM {$_POST['dbname']}"));
			else:
				$tb_arr=explode(",", $_POST['table']);
				foreach($tb_arr as $key):
					$tables[] = array($tb_prefix => $key);
				endforeach;
			endif;
			switch ($_POST['export_type']):
				//----------------
				//  导出为创建语句
				//----------------
				case "create_sql":
					require "export_create_sql.php";
					//循环所有表
					for ($i = 0; $i < count($tables); $i++):
						$sql = "SHOW CREATE TABLE " . $tables[$i][$tb_prefix];
						$result[] = $db->getRs($db->select($sql));
					endfor;
					//导出
					export_create_sql($result, $_POST['save_type'], $dbname = $_POST['dbname']);
					break;
				//----------------
				//  导出为结构预览
				//----------------
				case "struct_preview":
					require "export_struct_preview.php";
					//循环所有表
					for ($i = 0; $i < count($tables); $i++):
						$sql = "SHOW FULL COLUMNS FROM " . $tables[$i][$tb_prefix];
						$table_sql = "SHOW TABLE STATUS FROM " . $_POST['dbname'] . " WHERE Name='" . $tables[$i][$tb_prefix] . "'";
						$result[] = array("table" => $db->getRs($db->select($table_sql)), "columns" => $db->getArray($db->select($sql)));
					endfor;
					//导出
					export_struct_preview($result, $_POST['save_type'], $dbname = $_POST['dbname']);
					break;
				default:
					echo "请选择导出类型...";
			endswitch;
			break;
		case "mssql":
			//链接数据库
			require "../Include/MsSQL.class.php";
			$db = MsSQL::getInstance();
			$db->dbHost = $_POST['host'];
			$db->dbName = $_POST['dbname'];
			$db->dbUser = $_POST['uid'];
			$db->dbPwd = $_POST['pwd'];
			$db->open();
			switch ($_POST['export_type']):
				//----------------
				//  导出为结构预览
				//----------------
				case "struct_preview":
					require "mssql_export_struct_preview.php";
					$sql="SELECT o.name,CAST(ep.value AS NVARCHAR(MAX)) comment, convert(varchar(16),o.create_date,20) create_date FROM sys.all_objects o LEFT JOIN sys.schemas s ON o.schema_id = 
s.schema_id LEFT JOIN sys.tables t ON o.object_id = t.object_id LEFT JOIN sys.extended_properties ep ON (o.object_id = ep.major_id AND ep.class = 1 AND ep.minor_id = 0 AND ep.name = 
'MS_Description') LEFT JOIN (SELECT object_id, SUM(ROWS) row_count FROM sys.partitions WHERE index_id < 2 GROUP BY object_id) st ON o.object_id = st.object_id WHERE s.name = N'dbo' AND 
(o.type='U' OR o.type='S') ";
					$tables=$db->getArray($db->select($sql));
					for($i=0;$i<count($tables);$i++):
						$sql="SELECT o.name table_name,c.name column_name,t.name type_name, c.max_length,  c.is_nullable, c.is_identity, dc.definition default_definition,  CAST(ep.value AS NVARCHAR(MAX)) comment FROM sys.all_columns c LEFT JOIN sys.all_objects o ON c.object_id = o.object_id LEFT JOIN sys.schemas s ON o.schema_id = s.schema_id 
LEFT JOIN sys.types t ON c.user_type_id = t.user_type_id LEFT JOIN sys.all_objects do ON c.default_object_id = do.object_id LEFT JOIN sys.default_constraints dc ON c.default_object_id = 
dc.object_id LEFT JOIN sys.all_objects o3 ON c.rule_object_id = o3.object_id LEFT JOIN sys.identity_columns id ON c.object_id = id.object_id AND c.column_id = id.column_id LEFT JOIN 
sys.computed_columns cc ON c.object_id = cc.object_id AND c.column_id = cc.column_id LEFT JOIN sys.extended_properties ep ON (c.object_id = ep.major_id AND ep.class = 1 AND c.column_id = 
ep.minor_id AND ep.name = 'MS_Description') WHERE s.name = N'dbo' AND o.type IN ('U', 'S', 'V') AND o.name = N'".$tables[$i]['name']."' ORDER BY table_name";
						//echo $sql."\r\n";
						$tables[$i]['columns']=$db->getArray($db->select($sql));
					endfor;
					mssql_export_struct_preview($tables, $_POST['save_type'], $dbname = $_POST['dbname']);
				break;
			endswitch;
			break;
	endswitch;
endif;